package com.samphanie.auiu.fragment.home

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.samphanie.auiu.BR
import com.samphanie.auiu.R
import com.samphanie.auiu.databinding.LayoutHomeGoodsListItemOneBinding
import com.samphanie.auiu.databinding.LayoutHomeGoodsListItemTwoBinding
import com.samphanie.auiu.library.utils.AuiuDisplayUtil
import com.samphanie.auiu.model.GoodsModel
import com.samphanie.auiu.route.AuiuRoute
import com.samphanie.auiu.ui.item.AuiuDataItem
import com.samphanie.auiu.ui.item.AuiuViewHolder

/**
 * @author ZSY
 */
open class GoodsItem(val goodsModel: GoodsModel, val hotTab: Boolean) :
    AuiuDataItem<GoodsModel, GoodsItem.GoodsItemHolder>(goodsModel) {

    val MAX_TAG_SIZE = 3

    override fun onBindData(holder: GoodsItemHolder, position: Int) {
        val context = holder.itemView.context

        holder.binding.setVariable(BR.goodsModel, goodsModel)

//        holder.findViewById<ImageView>(R.id.item_image)?.loadUrl(goodsModel.sliderImage)
//        holder.findViewById<TextView>(R.id.item_title)?.text = goodsModel.goodsName
//        holder.findViewById<TextView>(R.id.item_price)?.text = selectPrice(goodsModel.marketPrice, goodsModel.groupPrice)
//        holder.findViewById<TextView>(R.id.item_sale_desc)?.text = goodsModel.completedNumText

        val itemLabelContainer = holder.itemView.findViewById<LinearLayout>(R.id.item_label_container)

        if (itemLabelContainer != null) {
            if (!goodsModel.tags.isNullOrBlank()) {
                itemLabelContainer.visibility = View.VISIBLE
                val split = goodsModel.tags.split(",")
                for (index in split.indices) {
                    val childCount = itemLabelContainer.childCount
                    if (index > MAX_TAG_SIZE - 1) {
                        //倒叙
                        for (index in childCount - 1 downTo MAX_TAG_SIZE - 1) {
                            // itemLabelContainer childcount =5
                            // 3，后面的两个都需要被删除
                            itemLabelContainer.removeViewAt(index)
                        }
                        break
                    }
                    // 解决上下滑动复用的问题
                    val labelView: TextView = if (index > childCount - 1) {
                        val view = createLabelView(context, index != 0)
                        itemLabelContainer.addView(view)
                        view
                    } else {
                        itemLabelContainer.getChildAt(index) as TextView
                    }
                    labelView.text = split[index]
                }
            } else {
                itemLabelContainer.visibility = View.GONE
            }
        }

        if (!hotTab) {
            val margin = AuiuDisplayUtil.dp2px(2f)
            val params = holder.itemView.layoutParams as RecyclerView.LayoutParams
            val parentLeft = auiuAdapter?.getAttachRecyclerView()?.left ?: 0
            val parentPaddingLeft = auiuAdapter?.getAttachRecyclerView()?.paddingLeft ?: 0
            val itemLeft = holder.itemView.left
            if (itemLeft == (parentLeft + parentPaddingLeft)) {
                params.rightMargin = margin
            } else {
                params.leftMargin = margin
            }
            holder.itemView.layoutParams = params
        }

        holder.itemView.setOnClickListener {
            val bundle = Bundle()
            bundle.putString("goodsId", goodsModel.id)
            bundle.putParcelable("goodsModel", goodsModel)
            AuiuRoute.startActivity(context, bundle, AuiuRoute.Destination.GOODS_DETAIL)
        }
    }

    private fun createLabelView(context: Context, withLeftMargin: Boolean): TextView {
        val labelView = TextView(context)
        labelView.setTextColor(ContextCompat.getColor(context, R.color.color_eed))
        labelView.textSize = 11f
        labelView.gravity = Gravity.CENTER
        val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            AuiuDisplayUtil.dp2px(16f)
        )
        params.leftMargin = if (withLeftMargin) AuiuDisplayUtil.dp2px(5f) else 0
        labelView.layoutParams = params
        return labelView
    }

    override fun onCreateViewHolder(parent: ViewGroup): GoodsItemHolder? {
        val inflater = LayoutInflater.from(parent.context)
        val binding = DataBindingUtil.inflate<ViewDataBinding>(inflater, getItemLayoutRes(), parent, false)
        return GoodsItemHolder(binding)
    }

//    override fun getItemView(parent: ViewGroup): View? {
//        val inflater = LayoutInflater.from(parent.context)
//        if (hotTab) {
//            binding = LayoutHomeGoodsListItemOneBinding.inflate(inflater, parent, false)
//        } else {
//            binding = LayoutHomeGoodsListItemTwoBinding.inflate(inflater, parent, false)
//        }
//        return binding.root
//    }

    override fun getItemLayoutRes(): Int {
        return if (hotTab) R.layout.layout_home_goods_list_item_one else R.layout.layout_home_goods_list_item_two
    }

    override fun getSpanSize(): Int {
        return if (hotTab) super.getSpanSize() else 1
    }

    class GoodsItemHolder(val binding: ViewDataBinding): AuiuViewHolder(binding.root) {

    }
}